package cn.tedu.luban.order.main.repository;

import cn.tedu.luban.order.main.converter.OrderConverter;
import cn.tedu.luban.order.main.dao.OrderDAO;
import cn.tedu.luban.order.protocol.bo.OrderBO;
import cn.tedu.luban.order.protocol.dos.OrderDO;
import cn.tedu.luban.order.protocol.param.OrderCreateParam;
import cn.tedu.luban.order.protocol.param.OrderParam;
import cn.tedu.luban.order.protocol.query.AdminOrderListQuery;
import cn.tedu.luban.order.protocol.query.FrontOrderDBPagerQuery;
import cn.tedu.luban.order.protocol.query.FrontOrderListQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OrderRepoImpl implements OrderRepository{
    @Autowired
    private OrderDAO orderDAO;
    @Autowired
    private OrderConverter orderConverter;
    @Override
    public void saveOrder(OrderCreateParam param) {
        //1. 转化成do
        OrderDO orderDO = orderConverter.param2do(param);
        //2. 新增写入
        orderDAO.saveOrder(orderDO);
    }
    @Override
    public Long countOrders(FrontOrderListQuery query) {
        //1. 转化成dbQuery
        FrontOrderDBPagerQuery dbQuery = orderConverter.query2dbQuery(query);
        //2. 调用持久层方法 select count(id) from order where user_id=#{} and status in (10,20,50)
        return orderDAO.countFrontOrders(dbQuery);
    }
    @Override
    public List<OrderBO> queryOrders(FrontOrderListQuery query) {
        //1. 转化成dbQuery
        FrontOrderDBPagerQuery dbQuery = orderConverter.query2dbQuery(query);
        //2. 持久层分页数据list select * from order where user_id=#{} and status in (10,20,50) order by gmt_create DESC limit 0,10
        List<OrderDO> dos = orderDAO.queryFrontOrders(dbQuery);
        //3. 转化成orderBOlist
        return orderConverter.dos2bos(dos);
    }
    @Override
    public OrderBO getOrderByOrderNo(String orderNo) {
        OrderDO orderDO = orderDAO.selectOrderByOrderNo(orderNo);
        return orderConverter.do2bo(orderDO);
    }
    @Override
    public Long updateSignStatus(OrderParam orderParam) {
        //1. 转化 param成do 将orderNo和status 拷贝到do 同时补充6个通用字段 更新的时候不实用create字段
        OrderDO orderDO = orderConverter.param2do(orderParam);
        //2. 调用专门更新签到的sql
        return orderDAO.updateOrderSignByOrderNo(orderDO);
    }
    @Override
    public Long updateConfirmStatus(OrderParam orderParam) {
        OrderDO orderDO = orderConverter.param2do(orderParam);
        return orderDAO.updateOrderConfirmByOrderNo(orderDO);
    }


    @Override
    public Long countOrders(AdminOrderListQuery adminOrderListQuery) {
        return null;
    }

    @Override
    public List<OrderBO> queryOrders(AdminOrderListQuery orderBOQuery) {
        return null;
    }

    @Override
    public OrderBO getOrderById(Long id) {
        return null;
    }

    @Override
    public void closeOrder(OrderParam param) {

    }



    @Override
    public void updateStatus(OrderParam param) {

    }


}
